
/**   
 * @Title: AppConfig.java 
 * @Package: com.aft.jlgh.app 
 * @Description: TODO
 * @author Mandarava  
 * @date 2016年5月16日 下午3:51:57 
 * @version 1.3.1 
 * @Email wuyan1688@qq.com
 */

package com.aft.terra.app;

import org.beetl.ext.jfinal.BeetlRenderFactory;


import com.aft.terra.common.interceptor.OverAllParamsInterceptor;
import com.aft.terra.common.interceptor.VerificationGameQualificationsInterceptor;
import com.aft.terra.common.utils.XSSDefenseFormat;
import com.aft.terra.modules.basic.SystemConfiguration;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.CaseInsensitiveContainerFactory;
import com.jfinal.plugin.activerecord.dialect.MysqlDialect;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.plugin.druid.DruidStatViewHandler;
import com.jfinal.plugin.ehcache.EhCachePlugin;
import com.jfinal.plugin.redis.RedisPlugin;

/**
 * @author Mandarava
 * @date 2016年5月16日 下午3:51:57
 * @version V1.3.1
 */

public class AppConfig extends JFinalConfig {

	/**
	 * @param me
	 * @see com.jfinal.config.JFinalConfig#configConstant(com.jfinal.config.Constants)
	 */
	@Override
	public void configConstant(Constants me) {
		loadPropertyFile(com.aft.terra.app.Constants.RES_JDBC_FILE_NAME);
		me.setMainRenderFactory(new BeetlRenderFactory());
		Boolean debug = Boolean.valueOf(getProperty("debug"));
		me.setDevMode(debug);
//		me.setBaseUploadPath(getProperty(com.aft.openAccount.app.Constants.UPLOAD_FILE_PATH));
		me.setError404View(com.aft.terra.app.Constants.ERROR_404_VIEW);
		me.setError500View(com.aft.terra.app.Constants.ERROR_500_VIEW);
	}

	/**
	 * @param arg0
	 * @see com.jfinal.config.JFinalConfig#configHandler(com.jfinal.config.Handlers)
	 */

	@Override
	public void configHandler(Handlers arg0) {
		arg0.add(new DruidStatViewHandler("/druid"));
	}

	/**
	 * @param arg0
	 * @see com.jfinal.config.JFinalConfig#configInterceptor(com.jfinal.config.Interceptors)
	 */

	@Override
	public void configInterceptor(Interceptors me) {
		me.add(new OverAllParamsInterceptor());
	}

	/**
	 * @param me
	 * @see com.jfinal.config.JFinalConfig#configPlugin(com.jfinal.config.Plugins)
	 */
	@Override
	public void configPlugin(Plugins me) {
		String url = getProperty(com.aft.terra.app.Constants.JDBC_URL);
		String userName = getProperty(com.aft.terra.app.Constants.JDBC_USER_NAME);
		String password = getProperty(com.aft.terra.app.Constants.JDBC_PASSWORD);
		String driverClass = getProperty(com.aft.terra.app.Constants.JDBC_DRIVER_CLASS);
		Integer maxActive = getPropertyToInt(com.aft.terra.app.Constants.JDBC_MAX_ACTIVE);
		String filters = getProperty(com.aft.terra.app.Constants.JDBC_FILTERS);

		DruidPlugin mysql = new DruidPlugin(url, userName, password, driverClass, filters);
		mysql.setMaxActive(maxActive);
		me.add(mysql);

		Boolean showSql = Boolean.valueOf(getProperty(com.aft.terra.app.Constants.IS_SHOW_SQL));
		ActiveRecordPlugin arp = new ActiveRecordPlugin(mysql);
		arp.setContainerFactory(new CaseInsensitiveContainerFactory(Boolean.TRUE));
		arp.setDialect(new MysqlDialect());
		arp.setShowSql(showSql);
		MappingKit.mapping(arp);
		me.add(arp);
		
		/**
		 * 用户session共享redis
		 */
		String redisLoginUser = getProperty(com.aft.terra.app.Constants.REDIS_DB_NAME);
		String redisLoginUserIp = getProperty(com.aft.terra.app.Constants.REDIS_IP_ADDRESS);
		Integer redisLoginUserPort = getPropertyToInt(com.aft.terra.app.Constants.REDIS_PORT);
		String redisPassword = getProperty(com.aft.terra.app.Constants.REDIS_DB_PASSWORD);
		RedisPlugin sessionRedis = new RedisPlugin(redisLoginUser, redisLoginUserIp, redisLoginUserPort, redisPassword);
		me.add(sessionRedis);

		me.add(new EhCachePlugin());
		BeetlRenderFactory.groupTemplate.registerFormat(com.aft.terra.app.Constants.CUSTOM_FORMAT_XSS_NAME, new XSSDefenseFormat());
	}

	/**
	 * @param routes
	 * @see com.jfinal.config.JFinalConfig#configRoute(com.jfinal.config.Routes)
	 */

	@Override
	public void configRoute(Routes routes) {
		routes.add(new ControllerRoute());
	}

	@Override
	public void afterJFinalStart() {
		SystemConfiguration.initConfiguration();
		// JobManager.initSysQuartz();
	}
}
